package com.lzxuni.modules.basics.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.basics.entity.BasicTree;
import com.lzxuni.modules.basics.entity.BasicsMap;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.service.FileEntityService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.basics.service.BasicsMapService;
import org.springframework.web.bind.annotation.RestController;
import com.lzxuni.modules.common.controller.BaseController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 基础信息_地图管理 前端控制器
 * </p>
 *
 * @author LaoNa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/Basics/Map")
public class BasicsMapController extends BaseController {

    @Autowired
    private BasicsMapService basicsMapService;
    private String templatesPath = "/Basics/Map";
    private String typeName = "基础信息_地图图片";
    private String ywType = "mapPic";
    @Autowired
    private FileEntityService fileEntityService;

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView(templatesPath + "/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination, String queryJson) {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        BasicsMap basicsMap = JSON.parseObject(queryJson, BasicsMap.class);
        PageData pageData = getPageData(basicsMapService.queryPage(pageParameter, basicsMap));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String keyValue, String queryJson) throws Exception {
        ModelAndView mv = new ModelAndView(templatesPath + "/form");
        BasicTree basicTree = JSON.parseObject(queryJson, BasicTree.class);
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(keyValue);
        fileBeanCustom.setYwType(ywType);
        mv.addObject("mapPicNum", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        mv.addObject("basicTree", basicTree);
        return mv;
    }

    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        basicsMapService.removeById(keyValue);
        fileEntityService.deleteByYwId(keyValue);
        return R.ok("删除成功");
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, BasicsMap basicsMap, String mapPic) throws Exception {

        basicsMap.setMapId(keyValue);
        if (StringUtils.isEmpty(basicsMap.getMapId())) {
            String ywId = UuidUtil.get32UUID();
            basicsMap.setMapId(ywId);
            if (!StringUtils.isEmpty(mapPic) && !"&amp;nbsp;".equals(mapPic)) {
                fileEntityService.insert(mapPic.replace("&quot;", "\""), ywId, typeName, ywType, null);
            }
            basicsMapService.save(basicsMap);
            return R.ok("保存成功");
        } else {
            fileEntityService.deleteByYwId(keyValue);
            if (!StringUtils.isEmpty(mapPic) && !"&amp;nbsp;".equals(mapPic)) {
                fileEntityService.insert(mapPic.replace("&quot;", "\""), keyValue, typeName, ywType, null);
            }
            basicsMapService.updateById(basicsMap);
            return R.ok("修改成功");
        }
    }


}

